import json


with open('../MC1/V1/MC1_NEW.json', encoding='utf-8') as f:
    j = json.load(f)
dict_name_to_node = {}
for n in j['nodes']:
    dict_name_to_node[n['id']] = n


def get_ralated_nodes_and_edges(node_name):
    related_nodes = []
    related_edges = []
    ls = ['source', "target"]

    for e in j['links']:
        for i in range(len(ls)):
            if e[ls[i]] == node_name:
                related_edges.append(e)
                related_nodes.append(dict_name_to_node[e[ls[1-i]]])
                break
    return related_nodes, related_edges


def get_and_save(node_name):
    """

    :param node_name:
    :return:
    """
    n, e = get_ralated_nodes_and_edges(node_name)
    dict_name_to_json = {"nodes": n, "edges": e}

    for key, value in dict_name_to_json.items():
        with open(f"results/{node_name}_{key}.json", 'w', encoding='utf-8') as f:
            json.dump(value, f, ensure_ascii=False, indent=True)


get_and_save("Faroe Islands Shrimp Shark")
